package com.rqf.mr.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.rqf.mr.service.IJdbcService;

@Service("JdbcService")
public class JdbcService implements IJdbcService {

	public static JdbcTemplate jdbcTemplate;
	
	 /**
	 * 新增
	 * @param tableName 表名
	 * @param inputMap 添加的字段值对应Map
	 * @param sequenceFun id自增长函数名
	 * @return
	 * @author ServerZhang
	 * @date 2016年3月25日
	 */
	@Override
	public Boolean add(String tableName,Map<String, Object> inputMap,
			String sequenceFun){
		Boolean isSuccess = true;
		if (StringUtils.isEmpty(tableName) || inputMap == null
				|| inputMap.size() <= 0) {
			return false;
		}
		StringBuffer fieldBuffer = new StringBuffer();
		StringBuffer valueBuffer = new StringBuffer();
		if (!StringUtils.isEmpty(sequenceFun)) {
			fieldBuffer.append("id");
			valueBuffer.append(sequenceFun).append(".nextval");
		}
		Set<String> keySet = inputMap.keySet();
		for (String key : keySet) {
			if (fieldBuffer.length() > 0) {
				fieldBuffer.append(",");
			}
			fieldBuffer.append(key);
			if (valueBuffer.length() > 0) {
				valueBuffer.append(",");
			}
			valueBuffer.append("'").append(inputMap.get(key)).append("'");
		}
		StringBuffer sqlBuffer = new StringBuffer();
		sqlBuffer.append("insert into ").append(tableName)
			.append("(").append(fieldBuffer).append(")")
			.append(" values(").append(valueBuffer).append(")");
		try {
			jdbcTemplate.execute(sqlBuffer.toString());
		} catch (Exception e) {
			isSuccess = false;
		}
		return isSuccess;
	}
	
	 /**
	 * 更新
	 * @param tableName
	 * @param inputMap
	 * @param whereStr
	 * @return
	 * @author ServerZhang
	 * @date 2016年3月25日
	 */
	@Override
	public Boolean update(String tableName,Map<String, Object> inputMap,
			String whereStr){
		Boolean isSuccess = true;
		if (StringUtils.isEmpty(tableName) || inputMap == null
				|| inputMap.size() <= 0 || StringUtils.isEmpty(whereStr)) {
			return false;
		}
		StringBuffer sqlBuffer = new StringBuffer();
		sqlBuffer.append("Update ").append(tableName).append(" set ");
		Set<String> keySet = inputMap.keySet();
		int index = 0;
		for (String key : keySet) {
			if (index != 0) {
				sqlBuffer.append(",");
			}
			sqlBuffer.append(key).append("='").append(inputMap.get(key)).append("'");
			index ++ ;
		}
		sqlBuffer.append(" where ").append(whereStr);
		try {
			jdbcTemplate.execute(sqlBuffer.toString());
		} catch (Exception e) {
			isSuccess = false;
		}
		return isSuccess;
	}
	
	 /**
	 * 删除记录
	 * @param tableName
	 * @param id
	 * @return
	 * @author ServerZhang
	 * @date 2016年3月25日
	 */
	@Override
	public Boolean deleteById(String tableName, Integer id){
		Boolean isSuccess = true;
		if (StringUtils.isEmpty(tableName) || id == null) {
			return false;
		}
		String sql = "Delete from "+tableName+" where id="+id;
		try {
			jdbcTemplate.execute(sql);
		} catch (Exception e) {
			isSuccess = false;
		}
		return isSuccess;
	}
	@Override
	public Boolean delete(String tableName, String whereStr){
		Boolean isSuccess = true;
		if (StringUtils.isEmpty(tableName) || StringUtils.isEmpty(whereStr)) {
			return false;
		}
		String sql = "Delete from "+tableName+" where "+whereStr;
		try {
			jdbcTemplate.execute(sql);
		} catch (Exception e) {
			isSuccess = false;
		}
		return isSuccess;
	}
	 /**
	 * 查询
	 * @param tableName
	 * @param fields
	 * @param whereStr
	 * @param sortStr
	 * @return
	 * @author ServerZhang
	 * @date 2016年3月25日
	 */
	@Override
	public List<Map<String, Object>> getList(String tableName, String fields,
			String whereStr, String sortStr){
		List<Map<String, Object>> returnList = new ArrayList<Map<String,Object>>();
		if (StringUtils.isEmpty(tableName)) {
			return returnList;
		}
		StringBuffer sqlBuffer = new StringBuffer();
		sqlBuffer.append("Select ");
		if (!StringUtils.isEmpty(fields)) {
			sqlBuffer.append(fields);
		}else {
			sqlBuffer.append("*");
		}
		sqlBuffer.append(" from ").append(tableName);
		if (!StringUtils.isEmpty(whereStr)) {
			sqlBuffer.append(" where ").append(whereStr);
		}
		if (!StringUtils.isEmpty(sortStr)) {
			sqlBuffer.append(" order by ").append(sortStr);
		}
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sqlBuffer.toString());
		return list;
	}
}
